home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / a3drot1a / readme.txt < prev   
Encoding:
Text File  |  1999-10-18  |  5.0 KB  |  104 lines

  1. Drawing a 3D Object
  2.  
  3. By: Tim Talma
  4.     ttalma@stny.rr.com
  5.  
  6. First let me start by saying that there are many faster ways to draw and
  7. transform a 3D object. The goal of this project is to help you learn the
  8. basics of 3D animation and what is invloved. The methods in this project
  9. are all standard routines that many 3D drawing programs use but with more
  10. refined algorithms, and many lookup tables. This method will work for 
  11. simple programs but for games etc. you will probably want to use somthing
  12. like DirectX, or openGL.
  13.  
  14.  
  15. I will skip an introducing you to the concept of 3D Drawing and rotation
  16. because there are plenty of these on the internet.
  17.  
  18. How this program works
  19.  
  20. 1. First a box is created using the center of the box as the origin (0,0,0)
  21. This box could have been created in a different way I can think of many
  22. better ones but I'll leave that up to you to implement.
  23.  
  24. 2. Next the number of points for each side is stored, so we know when to 
  25. stop drawing edges.
  26.  
  27. 3. The number of sides is saved so we know how many sides make the object.
  28.  
  29. 4. We find the normal for each object. The normals are import because they
  30. tell us what direction the plane is facing. I also reduced the normals to 
  31. have a length of one, to aid in shading, which I'll discuss later. To find
  32. out exactly how normals relate to plane lookup information on plane 
  33. equations.
  34.  
  35. 5. Create lookup tables. For this example I only created lookup tables for
  36. the sin and cos functions because these are by far the longest calculations
  37. in the project.
  38.  
  39. 6. Now comes the meat of the whole drawing and rotating the object. Right
  40. now we know where the object is floating in space about the origin, and we
  41. are looking at it from one viewpoint. so we need to determine what we will
  42. see when we rotate it. If we rotate it 0 degrees we will only see the front.
  43. but if rotate it we will see more than one side. So determine the rotation
  44. angle and which orign we want to rotate around. the formulas are as follows:
  45.  
  46. About X:
  47.     NewX = OldX
  48.     NewY = OldY * Cos(Angle) - OldZ * Sin(Angle)
  49.     NewZ = OldZ * Cos(Angle) + OldY * Sin(Angle)
  50.  
  51. About Y:
  52.     NewX = OldX * Cos(Angle) + OldZ * Sin(Angle)
  53.     NewY = OldY
  54.     NewZ = OldZ * Cos(Angle) - OldX * Sin(Angle)
  55.  
  56. About Z:
  57.     NewX = OldX * Cos(Angle) + OldY * Sin(Angle)
  58.     NewY = OldY * Cos(Angle) - OldX * Sin(Angle) 
  59.     NewZ = OldZ
  60.  
  61. After we calculate the new positions for the points we need to rotate the
  62. normal. We rotate the normal because this is faster than recalculating the
  63. normal for the new plane position. 
  64.     Next we determine if the plane is visible or not. This is done 
  65. with the normals. You multiple the normal with the position of the view 
  66. (commonly refered to as camera). If this value is greater than 0 the side 
  67. is visible. 
  68.     Then we find the color the side should be. This is again done with 
  69. the normals. First pick the light's position. Then find the 1's complement 
  70. vector (the vector with length of 1). and put these into the following 
  71. equation:
  72.     ambient * Max Light Value * (Plane normal * Lights 1's compliment)
  73.  
  74. The ambient value is how bright the object is even when there is not that
  75. much light. The Max Light Value is the brightest the light can be. I took
  76. only one value for the Normal depending on the view because I positioned
  77. the light at the same point as the view. So the 1's compliment would be
  78. (0,1,0) for the top view. and the normal * the 1's compliment would result
  79. in (0,normal.Y,0). This was done just to speed up the whole process.
  80.     Because the normal will go from 0 to 1 the closer to parallel to 
  81. the light source the surface becomes it will  go from darker to brighter.
  82. Giving the apperance of shading. 
  83.     Next choose the fill style for the object we are going to draw.
  84. To give the object a solid look choose solid, then assign the color. Now
  85. after all of that is done we are ready to draw one side of the object. I
  86. choose to use an API function to do this (Polygon (Window handler, 
  87. coordinates of points drawn, number of sides of object)). I choose this 
  88. method because all of the formulas I came up with that worked reliably
  89. were really slow (even on a PII450). But how they work is basicaly like
  90. so; find the top most point, find the next lowest point determine witch
  91. side it's on, find slope of line connecting these two points, store all 
  92. of the X values for this line in an array, find lowest point on opposite
  93. side of object, determine side, store all of the X values for this line 
  94. in an array, fill between these two lines by incrementing y by one and 
  95. drawing a line from the corisponding x points in the arrays, and repeat 
  96. untill object is filled. The Polygon function does all of this in one 
  97. line. After drawing the side change fillstyle back to transparent so none
  98. of the other objects get drawn in an odd way.
  99.  
  100. Well that is the quick explanation of how this program works. If you have 
  101. any questions just follow the code I tried to comment it pretty well, but
  102. if you still have questions e-mail me and I'll do my best to answer them.
  103.  
  104.